﻿//429. N 叉树的层序遍历
//给定一个 N 叉树，返回其节点值的层序遍历。（即从左到右，逐层遍历）。
//树的序列化输入是用层序遍历，每组子节点都由 null 值分隔（参见示例）。


/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root)
    {
        if (!root)
        {
            return {};
        }
        vector<vector<int>> ans;
        queue<Node*>  que;
        que.push(root);
        while (!que.empty())
        {
            vector<int> level;
            int cnt = que.size();
            for (int i = 0; i < cnt; i++)
            {
                Node* cur = que.front();
                que.pop();
                int size = cur->children.size();
                for (int i = 0; i < size; i++)
                {
                    que.push(cur->children[i]);
                }
                level.push_back(cur->val);
            }
            ans.push_back(level);
        }
        return ans;
    }
};